package com.suozhang.framework.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lijuwen
 * @email 1440919311@qq.com
 * @date 2017/8/31 15:19
 */

public class DateUtil {
    public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String FORMAT_YYYY_MM_DD_HH_MM3 = "yyyy年MM月dd日 HH:mm";
    public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String FORMAT_YYYY_MM_DD2 = "yyyy年MM月dd日";
    public static final String FORMAT_YYYY_MM = "yyyy-MM";
    public static final String FORMAT_YYYYMMDD = "yyyyMMdd";
    public static final String FORMAT_MM_DD_HH_MM = "MM-dd HH:mm";
    public static final String FORMAT_MM_DD = "MM-dd";
    public static final String FORMAT_HH_MM = "HH:mm";

    public static final long MINUTES = 60 * 1000;
    public static final long HOURS = 60 * MINUTES;
    public static final long DAY = 24 * HOURS;

    /**
     * 获取格式化后的时间 例如：yyyy-MM-dd HH:mm:ss --> yyyy-MM-dd
     * 默认源时间格式为yyyy-MM-dd HH:mm:ss
     *
     * @param sourceTime   源时间格式类型的字符串
     * @param targetFormat 目标时间对应的格式化类型
     * @return
     */
    public static String getFormatTime(String sourceTime, String targetFormat) {

        return getFormatTime(sourceTime, FORMAT_YYYY_MM_DD_HH_MM_SS, targetFormat);
    }

    /**
     * 获取格式化后的时间 例如：yyyy-MM-dd HH:mm:ss --> yyyy-MM-dd
     * 默认源时间格式为yyyy-MM-dd HH:mm:ss
     *
     * @param sourceTime   源时间格式类型的字符串
     * @param targetFormat 目标时间对应的格式化类型
     * @return
     */
    public static String getFormatTime2(String sourceTime, String targetFormat) {

        return getFormatTime(sourceTime, FORMAT_YYYY_MM_DD2, targetFormat);
    }

    /**
     * 获取格式化后的时间 例如：yyyy-MM-dd HH:mm:ss --> yyyy-MM-dd
     *
     * @param sourceTime   源时间格式类型的字符串
     * @param sourceFormat 源时间对应的格式化类型
     * @param targetFormat 目标时间对应的格式化类型
     * @return
     */
    public static String getFormatTime(String sourceTime, String sourceFormat, String targetFormat) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(targetFormat);
            return df.format(getDateTime(sourceTime, sourceFormat));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取格式化后的时间
     *
     * @param sourceTime
     * @param targetFormat
     * @return
     */
    public static String getFormatTime(Date sourceTime, String targetFormat) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(targetFormat);
            return df.format(sourceTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取Date类型的时间
     *
     * @param sourceTime   源时间格式类型的字符串
     * @param sourceFormat 源时间对应的格式化类型
     * @return
     */
    public static Date getDateTime(String sourceTime, String sourceFormat) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(TextUtils.isEmpty(sourceFormat) ? FORMAT_YYYY_MM_DD_HH_MM_SS : sourceFormat);
            return df.parse(sourceTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取Date类型的时间
     *
     * @param sourceTime
     * @param targetFormat
     * @return
     */
    public static Date getDateTime(Date sourceTime, String targetFormat) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(targetFormat);
            return df.parse(df.format(sourceTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 格式化时间到：刚刚，N小时前，昨天，Y天前
     * <p>
     * N<=24,Y<=3
     *
     * @param time
     * @return
     */
    public static String formatTimeToState(String time) {
        return formatTimeToState(getDateTime(time, FORMAT_YYYY_MM_DD_HH_MM_SS));
    }

    /**
     * 格式化时间到：刚刚，N小时前，昨天，Y天前
     * <p>
     * N<=24,Y<=3
     *
     * @param time
     * @return
     */
    public static String formatTimeToState(Date time) {
        if (time == null) {
            return "";
        }
        Date currentDate = new Date();
        long currentTime = currentDate.getTime();
        long targetTime = time.getTime();

        String defResultTime = getFormatTime(time, FORMAT_YYYY_MM_DD_HH_MM_SS);

        if (currentTime < targetTime) {
            return defResultTime;
        }

        boolean isYesterday = currentDate.getDay() - time.getDay() == 1;
        long intervalTime = currentTime - targetTime;

        if (isYesterday) {
            return "昨天";
        }

        if (intervalTime <= MINUTES * 5) {
            return "刚刚";
        }

        if (intervalTime <= HOURS * 24) {
            return intervalTime / HOURS + "小时前";
        }

        if (intervalTime <= DAY * 3) {
            return intervalTime / DAY + "天前";
        }

        return defResultTime;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    public static String stampToDate2(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /***
     *  -2:前天
     * -1：昨天
     * 0：今天
     * 1：明天
     * 2：后天
     * */
    public static String getDayString(long date) {
        SimpleDateFormat simpleDateHHMM = new SimpleDateFormat(FORMAT_HH_MM);
        SimpleDateFormat simpleDateMMDD = new SimpleDateFormat(FORMAT_MM_DD);
        SimpleDateFormat simpleDateMMDDHHMM = new SimpleDateFormat(FORMAT_MM_DD_HH_MM);
        Date startDate = new Date(date);
        String dateStr = "";
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (startDate.getTime() + offSet) / 86400000;
        int time = (int) (start - today);
        if (time == -2) {
            dateStr = "前天" + simpleDateHHMM.format(date);
        } else if (time == -1) {
            dateStr = "昨天" + simpleDateHHMM.format(date);
        } else if (time == 0) {
            dateStr = "今日" + simpleDateHHMM.format(date);
        } else if (time == 1) {
            dateStr = simpleDateMMDD.format(date) + "(明天)\n" + simpleDateHHMM.format(date);
        } else if (time == 2) {
            dateStr = simpleDateMMDD.format(date) + "(后天)\n" + simpleDateHHMM.format(date);
        } else {
            dateStr = simpleDateMMDDHHMM.format(date);
        }
        return dateStr;
    }


    /***
     *  -2:前天
     * -1：昨天
     * 0：今天
     * 1：明天
     * 2：后天
     * */
    public static String stringDayString(String str) {
        SimpleDateFormat main = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);

        SimpleDateFormat simpleDateHHMM = new SimpleDateFormat(FORMAT_HH_MM);
        SimpleDateFormat simpleDateMMDD = new SimpleDateFormat(FORMAT_MM_DD);
        SimpleDateFormat simpleDateMMDDHHMM = new SimpleDateFormat(FORMAT_MM_DD_HH_MM);
        long date= 0;
        try {
            date = main.parse(str).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Date startDate = new Date(date);
        String dateStr = "";
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (startDate.getTime() + offSet) / 86400000;
        int time = (int) (start - today);
        if (time == -2) {
            dateStr = "前天" + simpleDateHHMM.format(date);
        } else if (time == -1) {
            dateStr = "昨天" + simpleDateHHMM.format(date);
        } else if (time == 0) {
            dateStr = "今日" + simpleDateHHMM.format(date);
        } else if (time == 1) {
            dateStr = simpleDateMMDD.format(date) + "(明天)\t" + simpleDateHHMM.format(date);
        } else if (time == 2) {
            dateStr = simpleDateMMDD.format(date) + "(后天)\t" + simpleDateHHMM.format(date);
        } else {
            dateStr = simpleDateMMDDHHMM.format(date);
        }
        return dateStr;
    }

    /***
     *  -2:前天
     * -1：昨天
     * 0：今天
     * 1：明天
     * 2：后天
     * */
    public static String foramtArrivedTime(String str,int duration) {
        SimpleDateFormat simpleDateMMDDHHMM = new SimpleDateFormat(FORMAT_MM_DD_HH_MM);
        Calendar nowTime = Calendar.getInstance();//得到当前时间

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            nowTime.setTime(sdf.parse(str));//设置成这个时间
        } catch (ParseException e) {
            e.printStackTrace();
        }

        nowTime.add(Calendar.MINUTE, duration);//增加n分钟

        return String.format("请与%s前到达",simpleDateMMDDHHMM.format(nowTime.getTime()));
    }

    public static String cancleArrivedTime(String str) {
        SimpleDateFormat simpleDateMMDDHHMM = new SimpleDateFormat(FORMAT_MM_DD_HH_MM);
        Calendar nowTime = Calendar.getInstance();//得到当前时间

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            nowTime.setTime(sdf.parse(str));//设置成这个时间
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return sdf.format(nowTime.getTime());
    }
}
